package com.hhq.sso.system.controller;

import java.util.List;
import java.util.Map;

import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhq.sso.auth.base.pojo.SystemUserInfo;
import com.hhq.sso.auth.base.util.SystemUserContext;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.common.page.Query;
import com.hhq.sso.system.entity.Tenant;
import com.hhq.sso.system.service.ITenantService;

import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tenant")
@Hidden
@Tag(name = "租户管理", description = "接口")
public class TenantController {

    private ITenantService tenantService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入tenant")
    public CommonResult<Tenant> detail(Tenant tenant) {
        Tenant detail = tenantService.getOne(Condition.getQueryWrapper(tenant));
        return CommonResult.data(detail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "tenantId", description = "参数名称", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "tenantName", description = "角色别名", in = ParameterIn.QUERY,
            schema = @Schema(type = "string")),
        @Parameter(name = "contactNumber", description = "联系电话", in = ParameterIn.QUERY,
            schema = @Schema(type = "string"))})
    @Operation(summary = "分页", description = "传入tenant")
    public CommonResult<IPage<Tenant>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> tenant,
        Query query) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        QueryWrapper<Tenant> queryWrapper = Condition.getQueryWrapper(tenant, Tenant.class);
        IPage<Tenant> pages =
            tenantService.page(Condition.getPage(query), (!user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID))
                ? queryWrapper.lambda().eq(Tenant::getTenantId, user.getTenantId()) : queryWrapper);
        return CommonResult.data(pages);
    }

    /**
     * 下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "下拉数据源", description = "传入tenant")
    public CommonResult<List<Tenant>> select(Tenant tenant) {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        QueryWrapper<Tenant> queryWrapper = Condition.getQueryWrapper(tenant);
        List<Tenant> list = tenantService.list((!user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID))
            ? queryWrapper.lambda().eq(Tenant::getTenantId, user.getTenantId()) : queryWrapper);
        return CommonResult.data(list);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/page")
    @Operation(summary = "分页", description = "传入tenant")
    public CommonResult<IPage<Tenant>> page(Tenant tenant, Query query) {
        IPage<Tenant> pages = tenantService.selectTenantPage(Condition.getPage(query), tenant);
        return CommonResult.data(pages);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入tenant")
    public CommonResult<Boolean> submit(@Valid @RequestBody Tenant tenant) {
        return CommonResult.status(tenantService.saveTenant(tenant));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "逻辑删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(tenantService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 根据域名查询信息
     *
     * @param domain 域名
     */
    @GetMapping("/info")
    @Operation(summary = "配置信息", description = "传入domain")
    public CommonResult<Kv> info(String domain) {
        Tenant tenant = tenantService.getOne(Wrappers.<Tenant>query().lambda().eq(Tenant::getDomain, domain));
        Kv kv = Kv.init();
        if (tenant != null) {
            kv.set("tenantId", tenant.getTenantId()).set("domain", tenant.getDomain());
        }
        return CommonResult.data(kv);
    }

}
